import 'dart:io';
// import 'package:http/http.dart' as http;

typedef Future<List<String>> GetLinkHandler(File file);

class MigrateImage {
  var needHandleLinkList = List<String>();

  Future<void> scanImage({GetLinkHandler handler = handleGiteeFile}) async {
    var dir = Directory("../content/posts");
    var files = dir.listSync(recursive: true);
    for (var file in files) {
      if (FileSystemEntity.isFileSync(file.path) && file.path.endsWith(".md")) {
        var links = await handler(file);
        needHandleLinkList.addAll(links);
      }
    }
  }

  void migrate() {}
}

Future<List<String>> handleGiteeFile(File file) async {
  var urls = <String>[];
  var strs = file.readAsStringSync();
  var reg = RegExp("!\\[\\S+\\]\\((\\S+)\\)");
  var matches = reg.allMatches(strs);
  for (var m in matches) {
    var imageUrl = m.group(1);
    // print(m.group(0));
    print(imageUrl);

    if (imageUrl.startsWith("file:")) {
      continue;
    }
    if (!imageUrl.contains("gitee.com")) {
      continue;
    }
    // try {
    //   var response = await http.get(imageUrl);
    //   var length = response.bodyBytes.length;
    //   print("url : $imageUrl , size = $length");
    // } on Exception catch (e) {
    //   print(e);
    // }
  }
  return urls;
}
